﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Masters/AdminMasterPage.master" AutoEventWireup="true" CodeFile="OrderManager.aspx.cs" Inherits="Admin_OrderManager" %>
<%@ Register src="Parts/Pagging.ascx" tagname="Pagging" tagprefix="uc1" %>
<%@ Import Namespace="System.Linq.Dynamic" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Title" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
    <script type="text/javascript">
        $(document).ready(function () {

            $('a[name="deleteone"]').click(function () {
                if (confirm('Bạn có chắc muốn xóa phiếu đặt hàng này không?') == false) {
                    return false;
                }
            });

            $('#btndelete').click(function () {
                if ($('input[name="objId"]:checked').length <= 0) {
                    alert("Bạn chưa chọn phiếu đặt hàng cần xóa.");
                    return false;
                } else {
                    if (confirm('Bạn có chắc muốn xóa những phiếu đặt hàng được chọn không?') == false) {
                        return false;
                    } else {
                        $('#frmMain').attr('action', $('#deletehidden').val());
                        $(this).submit();
                    }

                }


            });
        });

</script>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" Runat="Server">

<div class="content-box"><!-- Start Content Box -->
				
		<div class="content-box-header">
					
			<h3>Quản lý phiếu đặt hàng</h3>
					
			<ul class="content-box-tabs">
				<li><a href="#tab1" class="default-tab">Danh sách</a></li>                
			</ul>
					
			<div class="clear"></div>
					
		</div> <!-- End .content-box-header -->
				
<div class="content-box-content">

<!-- Start Tab Danh Sach-->
<%
	         
              string act = "";
              int objId =0;
              bool isSuccess = false;
             
              if (Request.QueryString["act"] != null)
                  act = Request.QueryString["act"].ToString();
              if (Request.QueryString["id"] != null)
                  objId =Convert.ToInt32(Request.QueryString["id"].ToString());
             
    
              NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();
             
              if (act != "" && act == "delete")
              {
                  if (objId > 0)
                  {
                      //xóa 1
                      if ((from pdh in db.PhieuDatHangs
                           where pdh.MaPDH.Equals(objId)
                           select pdh).Count() > 0)
                      {
                          var querypdh = (from pdh in db.PhieuDatHangs
                                          where pdh.MaPDH.Equals(objId)
                                         select pdh).Single();

                          querypdh.MaTinhTrang = 6;                         
                          db.SubmitChanges();
                          isSuccess = true;
                         
                      }
                  }
                  else {
                      string objIdTemp = Request.Form["objId"];
                      if (objIdTemp != null)
                      {
                          string[] objIds = objIdTemp.Split(',');
                          foreach (string item in objIds)
                          {
                              int ma = Convert.ToInt32(item.Trim());
                              if ((from obj in db.PhieuDatHangs
                                   where obj.MaPDH.Equals(ma)
                                   select obj).Count() > 0)
                              {
                                  var queryObj = (from obj in db.PhieuDatHangs
                                                  where obj.MaPDH.Equals(ma)
                                                  select obj).Single();

                                  queryObj.MaTinhTrang = 6;     
                                  db.SubmitChanges();
                                  isSuccess = true;
                              }
                          }
                         
                      }
                  }
              }


              var query = from pdh in db.PhieuDatHangs 
                          orderby pdh.NgayDatHang descending 
                          select pdh;
             
              if (TrangThaiComboBox.SelectedValue.ToString()!="-1")
                  query = from pdh in db.PhieuDatHangs
                          where pdh.MaTinhTrang.Equals(TrangThaiComboBox.SelectedValue.Trim())
                          orderby pdh.NgayDatHang descending
                          select pdh;

              int trang = 1;
              if (Request.QueryString["Page"] != null)
                  trang = Convert.ToInt32(Request.QueryString["Page"].ToString());
              
              List<PhieuDatHang> lst = new List<PhieuDatHang>();
              if (query.Count<PhieuDatHang>() > 0)
              {
                  int nums = query.Count<PhieuDatHang>();
                  Session["TongSoDongDuLieu"] = nums;
                  Session["LinkPhanTrang"] = "OrderManager.aspx";
                  int soDongTrenTrang = Convert.ToInt32(Session["SoDongTrenTrang"]);
                  if (soDongTrenTrang <= 0)
                  {
                      soDongTrenTrang = 10;
                      Session["SoDongTrenTrang"] = 10;
                  }
                  int skip = (trang - 1) * soDongTrenTrang;
                  lst = query.Skip(skip).Take(soDongTrenTrang).ToList<PhieuDatHang>();
              }
              else
              {
                  Session["TongSoDongDuLieu"] = 0;
                  Session["SoDongTrenTrang"] = 10;
              }     

        
%>
<div class="tab-content default-tab" id="tab1"> 
<!-- This is the target div. id must match the href of this div's tab -->
					
<!-- Start Notification -->
    <% if (act != "" && isSuccess)
   {%>
<div class="notification <%if(isSuccess){%>success<%}else{%>attention<%}%> png_bg">
	<a href="javascript:void(0);" class="closeMessage">
        <img src="Images/icons/cross_grey_small.png" title="Đóng thông báo này" alt="Đóng" />
    </a>
	<div><% if (act == "delete")
         {%>Xóa thành công phiếu đặt hàng
	<%} %></div>
</div>
    <%} %>
<!-- End Notification -->
						
	<table align="center" width="100%">							
		<thead>
            <tr>
        				<td colspan="9">
					<div class="bulk-actions align-left">	
                        <asp:DropDownList ID="TrangThaiComboBox" runat="server"                           
                            AutoPostBack="True" DataSourceID="LinqDataSource1" 
                            DataTextField="TenTinhTrang" DataValueField="MaTinhTrang" 
                            ondatabound="TrangThaiComboBox_DataBound">
                        </asp:DropDownList>	
                        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
                            ContextTypeName="NhaKinhDoanhDataContext" EntityTypeName="" 
                            Select="new (MaTinhTrang, TenTinhTrang)" TableName="TTPhieuDatHangs">
                        </asp:LinqDataSource>
                         <input id="deletehidden" name="delete" type="hidden" value="OrderManager.aspx?act=delete&Page=<%=trang%>"/>
                         <% if (Convert.ToInt32(TrangThaiComboBox.SelectedValue) == 0 || Convert.ToInt32(TrangThaiComboBox.SelectedValue) == 1)
                            { %>
                        <input id="btndelete" type="submit" value="Xóa phiếu đặt hàng được chọn" class="button" />
                        <%} %>
					</div>
									
				</td>
			</tr>

			<tr>	
                <th><input class="check-all" type="checkbox"  name="checkall"/>
                </th>	
                <th>STT</th>
                <th>Khách hàng</th>	
				<th>Ngày đặt hàng</th>
				<th>Thành tiền</th>
				<th>Phí vận chuyển</th>				
                <th>Tình trạng</th>                
                <th>Nhà vận chuyển</th>               
                <th>Tác vụ</th>             
			</tr>
								
		</thead>
						 
		<tfoot>
			<tr>
				<td colspan="9">
					
<!-- Start Pagging -->
<uc1:pagging ID="Pagging1" runat="server" />
<!-- End Pagging -->
									
				</td>
			</tr>
		</tfoot>
						 
		<tbody>
            <% 
              int stt = 0;
           foreach (PhieuDatHang pdh in lst)
	      {
              stt++;
              string tennhavanchuyen = pdh.MaNhaVanChuyen.ToString();
              if ((from ob in db.NhaVanChuyens where ob.CarrierID.Equals(pdh.MaNhaVanChuyen) select ob).Count<NhaVanChuyen>() > 0) {
                  tennhavanchuyen = (from ob in db.NhaVanChuyens where ob.CarrierID.Equals(pdh.MaNhaVanChuyen) select ob).Single<NhaVanChuyen>().CarrierName;
              }
               
              string trangthaiPhieu = "";
              //load tất cả phieu
              if (TrangThaiComboBox.SelectedValue == "-1")
                  trangthaiPhieu = TrangThaiComboBox.Items.FindByValue(pdh.MaTinhTrang.ToString()).Text;
              else //load phieu theo trang thái
                  trangthaiPhieu = TrangThaiComboBox.Items.FindByValue(TrangThaiComboBox.SelectedValue).Text;
	      %>
			<tr>		
                <td><input type="checkbox" name="objId" value="<%=pdh.MaPDH%>" /></td>		
                <td><%=string.Format("{0:000}",stt) %></td>
                <td><a href="OrderDetails.aspx?act=edit&id=<%=pdh.MaPDH%>&Page=<%=trang%>" title="<%=pdh.KhachHang.TaiKhoan.TenDangNhap %>"><%=MD5Hash.Trim(pdh.KhachHang.TenKhachHang) %></a></td>
				<td><%=MD5Hash.Trim(pdh.NgayDatHang.ToString()) %></td>
				<td><%=MD5Hash.Trim(string.Format("{0:0.##}",pdh.ThanhTien)) %></td>
				<td><%=MD5Hash.Trim(string.Format("{0:0.##}",pdh.PhiVanChuyen)) %></td>              
                <td><%=MD5Hash.Trim(pdh.TTPhieuDatHang.TenTinhTrang)%></td>
                <td><%=MD5Hash.Trim(tennhavanchuyen)%></td>           
               
                <td>
                    <a href="OrderDetails.aspx?act=view&id=<%=pdh.MaPDH%>&Page=<%=trang%>" title="Xem chi tiết"><img src="Images/icons/details.png" alt="Xem chi tiết" /></a> 
                     <% if (pdh.MaTinhTrang == 0 || pdh.MaTinhTrang == 1)
                     { %>
                    <a name="deleteone" href="OrderManager.aspx?act=delete&id=<%=pdh.MaPDH%>&Page=<%=trang%>" title="Xóa"><img src="Images/icons/cross.png" alt="Xóa" /></a> 						
                    <%} %>
				</td>
			</tr>								
			
			<%}%>
		</tbody>							
	</table>						
</div>


<!-- End Tab Danh Sach-->					
 
   
					
</div> <!-- End .content-box-content -->
				
</div>

</asp:Content>

